import { message } from 'antd';
import { list, echo, edit, dele, listSpecial, exit, exitUndo } from '../services/gather';
import tools from '../../../utils';

const types = require('../../../assets/stureg_web/static/types.json');

export default {
  namespace: 'gather',

  state: {
    data: [],
    allData: [],

    // 表格记录条数
    counts: {
      total: 0,
      lodging1sex1: 0, // 住校男生人数
      lodging1sex0: 0, // 住校女生人数
      lodging0sex1: 0, // 跑堂男生人数
      lodging0sex0: 0, // 跑堂女生人数
    },

    success: true,
    echoModel: [],

    dataSpecial: [], // 开设专业数据(树数据)
    sysData: [], // 系部数据
    specData: {}, // 专业数据
    mappingSpecialName: {}, // 根据专业id获取专业名称
    mappingSpecialPID: {}, // 根据专业id获取系部id
    errMsg: '', // 错误信息
  },

  effects: {
    *list({ payload }, { call, put }) {
      const response = yield call(list, payload);
      yield put({ type: 'callbackList', response });
    },
    *listAll({ payload }, { call, put }) {
      const response = yield call(list, payload);
      yield put({ type: 'callbackList', response, isAll: true });
    },
    *echo({ payload }, { call, put }) {
      const response = yield call(echo, payload);
      yield put({ type: 'callbackEcho', response });
    },
    *edit({ payload }, { call, put }) {
      const response = yield call(edit, payload);
      yield put({ type: 'callbackSave', response });
    },
    *dele({ payload }, { call, put }) {
      const response = yield call(dele, payload);
      yield put({ type: 'callbackSave', response });
    },
    *exit({ payload }, { call, put }) {
      const response = yield call(exit, payload);
      yield put({ type: 'callbackSave', response });
    },
    *exitUndo({ payload }, { call, put }) {
      const response = yield call(exitUndo, payload);
      yield put({ type: 'callbackSave', response });
    },
    *listSpecial({ payload }, { call, put }) {
      const response = yield call(listSpecial, payload);
      yield put({ type: 'callbackListSpecial', response });
    },
  },

  reducers: {
    callbackList(st, { response, isAll }) {
      const state = st;
      if (response.success) {
        const data = response.data
          .map((item) => {
            const { specialId, type } = item;
            let specialName = state.mappingSpecialName[specialId];
            specialName += ' - ' + types[type];

            return {
              ...item,
              specialName,
            };
          });

        state.counts = response.counts;
        if (isAll) state.allData = data;
        else state.data = data;
      }
      return { ...state };
    },
    callbackSave(st, { response }) {
      const { success, status, msg } = response;
      const state = st;
      state.success = success;
      state.errMsg = '';

      if (success) {
        if (!status) message.success(msg).then();
        else state.errMsg = msg;
      }

      return { ...state };
    },
    callbackEcho(st, { response }) {
      const state = st;
      if (response.success) {
        state.echoModel = response.model;
        state.echoModel.auditAdminName = response.auditAdminName;
      }
      return { ...state };
    },
    callbackListSpecial(st, { response }) {
      const state = st;
      if (response.success) {
        const { data } = response;

        state.sysData = [];
        state.specData = {};
        const allData = [];
        const mappingSpecialName = {}; // 根据专业id获取专业名称
        const mappingSpecialPID = {}; // 根据专业id获取系部id

        for (let i = 0; i < data.length; i += 1) {
          const item = data[i];
          const { id, pid, name } = item;
          let { count } = item;
          count -= 0;
          const isSpec = pid > 0; // 系部或专业: 0系部, >0专业
          mappingSpecialName[id] = name;

          if (isSpec) {
            if (!state.specData[pid]) state.specData[pid] = [];
            state.specData[pid].push(item);
            mappingSpecialName[id] = name;
            mappingSpecialPID[id] = pid;
          } else state.sysData.push(item);

          allData.push({
            ...item,
            key: id,
            title: name,
            count,
            selectable: true, // "系部"可选择
          });
        }

        state.mappingSpecialName = mappingSpecialName;
        state.mappingSpecialPID = mappingSpecialPID;

        // "系部"显示学生人数
        state.dataSpecial = tools.funcPidToChildren(
          allData,
          'id',
          'pid',
          'children',
          true,
        )
          .map((item) => {
            const { pid, count, children = [] } = item;
            const isSpec = pid > 0; // 系部或专业: 0系部, >0专业
            let sum = 0, ids = [];

            if (isSpec) sum = count;
            else {
              children.map((childItem) => {
                sum += childItem.count;
                ids.push(childItem.id);
              });
            }

            return {
              ...item,
              count: sum,
              ids,
            };
          });
      }
      return { ...state };
    },
  },
};
